package com.anxin.act.task.entity;

import com.anxin.act.task.vo.TaskVo;
import com.anxin.act.utils.ActProcessUtils;
import com.anxin.act.utils.ActTaskUtils;
import com.anxin.act.utils.ActUtils;
import com.anxin.common.utils.DateUtils;
import com.anxin.common.utils.StringUtils;
//import io.swagger.annotations.ApiModel;
//import io.swagger.annotations.ApiModelProperty;
import org.activiti.engine.impl.pvm.process.ActivityImpl;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskInfo;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import com.anxin.framework.aspectj.lang.annotation.Excel;
import com.anxin.framework.web.entity.BaseEntity;
import java.util.Date;

/**
 * 加签任务对象 wf_act_ru_task
 *
 * @author jbs
 * @date 2021-10-22
 */
//@ApiModel(description = "加签任务")
public class WfActRuTask extends BaseEntity<WfActRuTask>{
    private static final long serialVersionUID = 1L;

    /** 流程实例ID */
    //@ApiModelProperty(value = "流程实例ID",position=20)
    private String actId;

    /** 原始的activiti任务的ID */
    //@ApiModelProperty(value = "原始的activiti任务的ID",position=30)
    private String actTaskId;

    /** 原始的activiti任务的taskKey */
    //@ApiModelProperty(value = "原始的activiti任务的taskKey",position=40)
    private String actTaskKey;

    /** 待办标题 */
    //@ApiModelProperty(value = "待办标题",position=50)
    private String actName;

    /** 加签类型 */
    //@ApiModelProperty(value = "加签类型",position=50)
    private String type;

    /** 来源类型 */
    //@ApiModelProperty(value = "来源类型",position=60)
    private String sourceType;

    /** 来源任务ID */
    //@ApiModelProperty(value = "来源任务ID",position=70)
    private String sourceId;

    /** 开始时间 */
    //@ApiModelProperty(value = "开始时间",position=80)
    private Date startTime;

    /** 审批人 */
    //@ApiModelProperty(value = "审批人",position=90)
    private String assignee;


    /** 是否可以输入审批结果 */
    //@ApiModelProperty(value = "是否可以输入审批结果",position=120)
    private String approvalResults;

    /**
     * 是否挂起 Y:挂起  N：正常
     */
    //@ApiModelProperty(value = "是否挂起 Y:挂起  N：正常",position=120)
    private String hang;

    /**
     * 是否可以修改表单，默认同加签节点
     */
    private String actModifyFrom;

    public WfActRuTask(){

    }

    /**
     * 初始化
     * @param wfActHiTask
     */
    public WfActRuTask(WfActHiTask wfActHiTask){
        this.actId = wfActHiTask.getActId();
        this.id = wfActHiTask.getId();
        this.actTaskId = wfActHiTask.getActTaskId();
        this.actTaskKey = wfActHiTask.getActTaskKey();
        this.actName = wfActHiTask.getActName();
        this.type = wfActHiTask.getType();
        this.sourceType = wfActHiTask.getSourceType();
        this.sourceId = wfActHiTask.getSourceId();
        this.assignee = wfActHiTask.getAssignee();
        this.approvalResults = wfActHiTask.getApprovalResults();

        this.startTime = DateUtils.getNowDate();

    }


    /**
     * 通过待办任务初始化
     * @param taskVo 待办任务
     */
    public WfActRuTask(TaskVo taskVo){
        Task task = ActTaskUtils.getTask(taskVo.getTaskId());
        ActivityImpl activityImpl = ActUtils.getActivityImpl(task);

        boolean addBeforeTaskRejected = "true".equals(activityImpl.getProperties().get("addbeforetaskrejected"));

        // 获取对应流程实例信息
        //ProcessInstance processInstance = ActProcessUtils.getProcessInstance(task.getProcessInstanceId());
        this.actId = task.getProcessInstanceId();
        this.actTaskId = task.getId();
        this.actTaskKey = task.getTaskDefinitionKey();
        this.sourceId = taskVo.getId();
        this.actName = this.getAddTaskName(task.getName(),taskVo);
        this.startTime = new Date(System.currentTimeMillis());
        this.sourceType = taskVo.getViewRuTask().getType();
        this.type = taskVo.getCompleteType();
        this.approvalResults = ("addBefore".equals(taskVo.getCompleteType()) && !addBeforeTaskRejected) ? "N" : "Y";
        this.hang = "N";
        this.actModifyFrom = taskVo.getAddTaskModifyFrom();

    }

    /**
     * 获取任务名称
     * @param taskName 任务名称
     * @param taskVo 待办任务对象
     * @return
     */
    public String getAddTaskName(String taskName,TaskVo taskVo){

        if (StringUtils.isNotBlank(taskVo.getNewTaskName())){
            return taskVo.getNewTaskName();
        }

        if ("申请人补充材料".equals(taskVo.getFormFieldValue("approved"))){
            return taskVo.getFormFieldValueText("approved");
        }

        if ("addBefore".equals(taskVo.getCompleteType())){
            return taskName + "(前加签)";
        }else if("addAfter".equals(taskVo.getCompleteType())){
            return taskName + "(后加签)";
        }else if("entrust".equals(taskVo.getCompleteType())){
            return taskName + "(转签)";
        }

        return taskName;
    }


    /**
     * 通过加签任务初始化
     * @param wfActRuTask
     */
    public WfActRuTask(WfActRuTask wfActRuTask){

    }



    public void setActId(String actId){
        this.actId = actId;
    }

    public String getActId(){
        return actId;
    }
    public void setActTaskId(String actTaskId){
        this.actTaskId = actTaskId;
    }

    public String getActTaskId(){
        return actTaskId;
    }
    public void setActTaskKey(String actTaskKey){
        this.actTaskKey = actTaskKey;
    }

    public String getActTaskKey(){
        return actTaskKey;
    }
    public void setActName(String actName){
        this.actName = actName;
    }

    public String getActName(){
        return actName;
    }
    public void setSourceType(String sourceType){
        this.sourceType = sourceType;
    }

    public String getSourceType(){
        return sourceType;
    }
    public void setSourceId(String sourceId){
        this.sourceId = sourceId;
    }

    public String getSourceId(){
        return sourceId;
    }
    public void setStartTime(Date startTime){
        this.startTime = startTime;
    }

    public Date getStartTime(){
        return startTime;
    }
    public void setAssignee(String assignee){
        this.assignee = assignee;
    }

    public String getAssignee(){
        return assignee;
    }

    public void setApprovalResults(String approvalResults){
        this.approvalResults = approvalResults;
    }

    public String getApprovalResults(){
        return approvalResults;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getHang() {
        return hang;
    }

    public void setHang(String hang) {
        this.hang = hang;
    }

    public String getActModifyFrom() {
        return actModifyFrom;
    }

    public void setActModifyFrom(String actModifyFrom) {
        this.actModifyFrom = actModifyFrom;
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)
            .append("id", getId())
            .append("actId", getActId())
            .append("actTaskId", getActTaskId())
            .append("actTaskKey", getActTaskKey())
            .append("actName", getActName())
            .append("sourceType", getSourceType())
            .append("sourceId", getSourceId())
            .append("startTime", getStartTime())
            .append("assignee", getAssignee())
            .append("approvalResults", getApprovalResults())
            .toString();
    }
}
